//
//  BrezzeTimeManage.m
//  Brezze France
//
//  Created by IOS on 2021/8/23.
//  Copyright © 2021 阿炎. All rights reserved.
//

#import "BrezzeTimeManage.h"

#define BrezzeTimeKey @"BrezzeTimeManage"
#define BrezzeTimePhoneKey @"BrezzeTimePhoneKey"

#define BrezzeLoginPhone @"BrezzeLoginPhone"
#define BrezzeLoginCode @"BrezzeLoginCode"
#define BrezzeLoginEmail @"BrezzeLoginEmail"

@implementation BrezzeTimeManage

+ (BrezzeTimeManage *)share {
    static BrezzeTimeManage *man = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        man = [[BrezzeTimeManage alloc] init];
    });
    return man;
}

- (NSString *)phone {
    
    NSString *p = [[NSUserDefaults standardUserDefaults] stringForKey:BrezzeTimePhoneKey];
    if (p.length > 0) {
        return p;
    }
    return @"";
}

- (void)setPhone:(NSString *)phone {
    if (phone.length > 0) {
        
        [[NSUserDefaults standardUserDefaults] setObject:phone forKey:BrezzeTimePhoneKey];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

- (NSString *)loginCode {
    NSString *p = [[NSUserDefaults standardUserDefaults] stringForKey:BrezzeLoginCode];
    if (p.length > 0) {
        return p;
    }
    return @"";
}

- (void)setLoginCode:(NSString *)loginCode {
    if (loginCode.length > 0) {
        
        [[NSUserDefaults standardUserDefaults] setObject:loginCode forKey:BrezzeLoginCode];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

- (NSString *)loginPhone {
    NSString *p = [[NSUserDefaults standardUserDefaults] stringForKey:BrezzeLoginPhone];
    if (p.length > 0) {
        return p;
    }
    return @"";
}

- (NSString *)loginEmail {
    NSString *p = [[NSUserDefaults standardUserDefaults] stringForKey:BrezzeLoginEmail];
    if (p.length > 0) {
        return p;
    }
    return @"";
}

- (void)setLoginPhone:(NSString *)loginPhone {
    if (loginPhone.length > 0) {
        
        [[NSUserDefaults standardUserDefaults] setObject:loginPhone forKey:BrezzeLoginPhone];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

- (void)setLoginEmail:(NSString *)loginEmail {
    if (loginEmail.length > 0) {
        
        [[NSUserDefaults standardUserDefaults] setObject:loginEmail forKey:BrezzeLoginEmail];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}


#pragma mark - 添加Key
+(void)addObjectIndateDic:(NSString*)key{
    datedic = [[NSMutableDictionary alloc]  initWithDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:BrezzeTimeKey]];
    if (!datedic) {
        datedic = [[NSMutableDictionary alloc] initWithCapacity:1];
    }
    NSDate* nowdate = [NSDate date];
    [datedic setObject:nowdate forKey:key];
    [[NSUserDefaults standardUserDefaults] setObject:datedic forKey:BrezzeTimeKey];
    //    DbgLog(@"添加了一个新的键值对：%@",datedic);
}

#pragma mark - 获取key的时间于当前时间比较
+(NSTimeInterval)getimeIntervalBy:(NSString*)key{
    if (!datedic) {
        datedic = [[NSMutableDictionary alloc] initWithDictionary:[[NSUserDefaults standardUserDefaults] objectForKey:BrezzeTimeKey]];
    }
    NSTimeInterval timeInterval = [[datedic objectForKey:key] timeIntervalSinceNow];
    //    DbgLog(@"与当前的时间差：%f",timeInterval);
    return timeInterval;
}

#pragma mark - 获取key的时间
+(NSTimeInterval)getimeIntervalBy:(NSString*)key wirhMax:(NSInteger)maxTimer{
    
    if (!datedic) {
        datedic = [[NSMutableDictionary alloc]initWithDictionary:[[NSUserDefaults standardUserDefaults]objectForKey:BrezzeTimeKey]];
    }
    if ([datedic objectForKey:key]) {
        if (maxTimer+[self getimeIntervalBy:key] < 0) {
            [datedic removeObjectForKey:key];
            return 0;
        }
        return maxTimer+[self getimeIntervalBy:key];
    }else{
        return 0;
    }
}

#pragma mark - 移除key的时间记录
+(void)removeObjectKey:(NSString*)key {
//    NSInteger error = [errorCode integerValue];
    if (!datedic) {
        datedic = [[NSMutableDictionary alloc]initWithDictionary:[[NSUserDefaults standardUserDefaults]objectForKey:BrezzeTimeKey]];
    }
    [datedic removeObjectForKey:key];
    [[NSUserDefaults standardUserDefaults] setObject:datedic forKey:BrezzeTimeKey];
}

/**
 * app开启一个定时器
 */
static  NSTimer *appTimewInformation;
+(void)starappTimeInformation{
    
    if (!appTimewInformation) {
        appTimewInformation = [NSTimer scheduledTimerWithTimeInterval:1 target:[BrezzeTimeManage share] selector:@selector(timeDoit) userInfo:nil repeats:YES];
    }else{
        if (![appTimewInformation isValid])return ;
        [appTimewInformation setFireDate:[NSDate distantPast]];
    }
}


/**
 * 关闭app全局定时器
 */
+(void)stopappTimeInformation{
    if (![appTimewInformation isValid]) {
        return ;
    }
    [appTimewInformation setFireDate:[NSDate distantFuture]];
    appTimewInformation = nil;
    
}

-(void)timeDoit{
    [[NSNotificationCenter defaultCenter] postNotificationName:APPTIMEINFORMATION object:self userInfo:nil];
}



@end
